package testJdk.stream;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @version 1.0.0
 * Created by cyd on 2021-12-29 17:56:10
 * @ClassName : learn_jdk.stream.TestSort
 * @Description : 类描述
 */
public class TestSort {


    /**
     * 自然排序
     */
    @Test
    public void test01(){
        ArrayList<String> list = Lists.newArrayList("GOV","BIZ");
        List<String> result = list.stream()
                .sorted(Comparator.comparing(String::toString))
                .collect(Collectors.toList());
        System.out.println(result);
    }

    /**
     * 倒叙
     */
    @Test
    public void test02(){
        ArrayList<String> list = Lists.newArrayList("GOV","BIZ");
        List<String> result = list.stream()
                .sorted(Comparator.comparing(String::toString).reversed())
                .collect(Collectors.toList());
        System.out.println(result);
    }


    /**
     * 测试去重
     */
    @Test
    public void testDistinct() {
        ArrayList<Student> students = Lists.newArrayList(
                new Student("001", "Tom"),
                new Student("002", "Tom"),
                new Student("001", "Tom"));
        System.out.println(students);
        // 去除完全一摸一样
        List<Student> studentList = students.stream().distinct().collect(Collectors.toList());
        System.out.println(studentList);
        // 去除名字相同,规则:先出现的保存，后出现的被去掉
        List<Student> studentList2 = students.stream().
                collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.
                                comparing(Student::getName))), ArrayList::new));
        System.out.println(studentList2);
    }

    @Test
    public void testSort() {
        List<Student> list = new ArrayList<>();
        Date birthday = new Date();
        for (int i = 0; i < 10; i++) {
            Student student = new Student();
            student.setName("苹果" + (10 - i));
            student.setStuNo("编号：" + (10 - i));
            student.setBirthday(DateUtil.offsetDay(birthday, -i));
            list.add(student);
        }
        System.out.println(list);
        System.out.println("---------经过正排序后--------");
        List<Student> students = list.stream().
                sorted(Comparator.comparing(Student::getBirthday)).
                collect(Collectors.toList());
        System.out.println(students);

        // list.stream().sorted(Comparator.comparing(Student::getAge).reversed())
        System.out.println("---------经过逆排序后--------");
        List<Student> students02 = list.stream().
                sorted(Comparator.comparing(Student::getBirthday).reversed()).
                collect(Collectors.toList());
        System.out.println(students02);

    }
}


@Data
@AllArgsConstructor
@NoArgsConstructor
class Student {
    private String stuNo;
    private String name;
    private Date birthday;

    public Student(String stuNo, String name) {
        this.stuNo = stuNo;
        this.name = name;
    }
}